import seal from '@/lib/request/seal'
import { RootState } from '../index'
import { Commit } from 'vuex'
import { Message } from 'element-ui'
import { IResponse } from '@/lib/service'

interface IRevokeSealData {
  applyReason: string
  sealApplyFileKey?: string
  sealApplyId?: number
  sealSn: string
  reason: string
  fileKey: string
}

export interface RevokeApplyState {
  sealList: any[]
  total: number
  revokeVisible: boolean
  detailVisible: boolean
  loading: false
  orgList: any[]
  auditOrgList: any[]
  applyData?: any
  isEdit: boolean
  saveLoading: boolean
  title: string
}
const initState: RevokeApplyState = {
  sealList: [],
  total: 0,
  revokeVisible: false,
  detailVisible: false,
  loading: false,
  orgList: [],
  auditOrgList: [],
  applyData: null,
  isEdit: false,
  saveLoading: false,
  title: '',
}

const getters = {}

const actions = {
  async getList(
    context: {
      commit: Commit
      state: RevokeApplyState
      rootState: RootState
      getters: any
    },
    payload: any
  ) {
    context.commit('setStateData', { loading: true })
    const data: IResponse = await seal
      .querySealApplyByAgent(
        payload.pageIndex,
        payload.pageSize,
        payload.sealName,
        payload.status,
        payload.sealType,
        2,
        payload.sealApplyId
      )
      .catch((e: any) => {
        context.commit('setStateData', {
          loading: false,
          sealList: [],
          total: 0,
        })
      })
    context.commit('setStateData', { loading: false })
    if (data && data.data) {
      context.commit('setStateData', {
        sealList: data.data.list || [],
        total: data.data.total,
      })
    }
  },
  saveSeal(
    context: {
      commit: Commit
      state: RevokeApplyState
      rootState: RootState
      getters: any
    },
    payload: IRevokeSealData
  ) {
    return new Promise(async (resolve, reject) => {
      context.commit('setStateData', { saveLoading: true })
      await seal
        .revokeSealApply(
          payload.sealSn,
          payload.reason,
          payload.sealApplyId,
          payload.fileKey
        )
        .then((data: IResponse) => {
          Message.success('保存成功')
          context.commit('setStateData', {
            applyData: null,
            revokeVisible: false,
            saveLoading: false,
          })
          resolve(data)
        })
        .catch(e => {
          context.commit('setStateData', { saveLoading: false })
          reject(e)
        })
    })
  },
  saveAndSubmit(
    context: {
      commit: Commit
      state: RevokeApplyState
      rootState: RootState
      getters: any
    },
    payload: IRevokeSealData
  ) {
    return new Promise(async (resolve, reject) => {
      seal
        .revokeSealApplySaveAndSubmit(
          payload.sealSn,
          payload.reason,
          payload.sealApplyId,
          payload.fileKey
        )
        .then((data: IResponse) => {
          Message.success('保存并提交成功')
          context.commit('setStateData', {
            applyData: null,
            revokeVisible: false,
          })
          resolve(data)
        })
        .catch(e => {
          reject(e)
        })
    })
  },
  submitSeal(
    context: {
      commit: Commit
      state: RevokeApplyState
      rootState: RootState
      getters: any
    },
    payload: number
  ) {
    return new Promise(async (resolve, reject) => {
      seal
        .sealApplySubmit(payload)
        .then((data: IResponse) => {
          Message.success('提交成功')
          resolve(data)
        })
        .catch(e => {
          reject(e)
        })
    })
  },
  sealDetail(
    context: {
      commit: Commit
      state: RevokeApplyState
      rootState: RootState
      getters: any
    },
    payload: number
  ) {
    return new Promise((resolve, reject) => {
      seal.querySealApplyDetail(payload).then((res: IResponse) => {
        resolve(res.data)
      })
    })
  },
  deleteApply(
    context: {
      commit: Commit
      state: RevokeApplyState
      rootState: RootState
      getters: any
    },
    payload: number
  ) {
    return new Promise((resolve, reject) => {
      seal.deleteApply(payload).then((data: IResponse) => {
        resolve()
      })
    })
  },

  revokeApplyCheck(
    context: {
      commit: Commit
      state: RevokeApplyState
      rootState: RootState
      getters: any
    },
    payload: string
  ) {
    return seal.revokeApplyCheck(payload).then((res: IResponse) => res.data)
  },
  sealRecall(
    context: {
      commit: Commit
      state: RevokeApplyState
      rootState: RootState
      getters: any
    },
    payload: number
  ) {
    return new Promise((resolve, reject) => {
      seal.sealRecall(payload).then((data: IResponse) => {
        resolve()
      })
    })
  },
}

const mutations = {
  setStateData(state: RevokeApplyState, data: any) {
    // eslint-disable-next-line no-param-reassign
    state = Object.assign(state, data)
  },
}

export default {
  namespaced: true,
  state: initState,
  getters,
  actions,
  mutations,
}
